package com.ouheng.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ouheng.shop.dao.AxImageDao;
import com.ouheng.shop.dao.LebeProductDao;
import com.ouheng.shop.dao.LebeProductPropertyDao;
import com.ouheng.shop.domain.AxImageDO;
import com.ouheng.shop.domain.LebeProductDO;
import com.ouheng.shop.service.LebeProductService;



@Service
public class LebeProductServiceImpl implements LebeProductService {
	@Autowired
	private LebeProductDao lebeProductDao;
	
	@Autowired
	private AxImageDao axImageDao;
	
	@Autowired
	private LebeProductPropertyDao lebeProductPropertyDao;
	
	@Override
	public LebeProductDO get(Long id){
		return lebeProductDao.get(id);
	}
	
	@Override
	public List<LebeProductDO> list(Map<String, Object> map){
		return lebeProductDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return lebeProductDao.count(map);
	}
	
	@Override
	public int save(LebeProductDO lebeProduct){
		return lebeProductDao.save(lebeProduct);
	}
	
	@Override
	public int update(LebeProductDO lebeProduct){
		return lebeProductDao.update(lebeProduct);
	}
	
	@Override
	@Transactional
	public int remove(Long id){
		lebeProductPropertyDao.removeByPid(id);
		Map<String, Object> map = new HashMap<>();
		map.put("pkid", id);
		map.put("type", "2");
		axImageDao.removeByPkIdAndType(map);
		return lebeProductDao.remove(id);
	}
	
	@Override
	@Transactional
	public int batchRemove(Long[] ids){
		for (Long long1 : ids) {
			lebeProductPropertyDao.removeByPid(long1);
			Map<String, Object> map = new HashMap<>();
			map.put("pkid", long1);
			map.put("type", "2");
			axImageDao.removeByPkIdAndType(map);
		}
		return lebeProductDao.batchRemove(ids);
	}

	@Override
	public int saveImage(LebeProductDO lebeProduct, List<Object> list,List<Object> list1) {
		Map<String, Object> map = new HashMap<>();
		map.put("pkid", lebeProduct.getId());
		map.put("type", 2);
		axImageDao.removeByPkIdAndType(map);
		
		Map<String, Object> map1 = new HashMap<>();
		map1.put("pkid", lebeProduct.getId());
		map1.put("type", 4);
		axImageDao.removeByPkIdAndType(map);
		int sum = lebeProductDao.update(lebeProduct);
		if (!list.isEmpty()) {
            for (Object obj : list) {
                if (obj != null) {
                	AxImageDO axImageDO = (AxImageDO) obj;
                    if (com.ouheng.common.utils.StringUtils.isNoneBlank(axImageDO.getSrc())) {
                    	axImageDO.setPkid(lebeProduct.getId());
                    	axImageDO.setSrc(axImageDO.getSrc());
                    	axImageDO.setType("2");
                    	axImageDO.setCreatedate(new Date());
                    	axImageDao.save(axImageDO);
                    }
                }
            }
        }
		if (!list1.isEmpty()) {
            for (Object obj : list1) {
                if (obj != null) {
                	AxImageDO axImageDO = (AxImageDO) obj;
                    if (com.ouheng.common.utils.StringUtils.isNoneBlank(axImageDO.getSrc())) {
                    	axImageDO.setPkid(lebeProduct.getId());
                    	axImageDO.setSrc(axImageDO.getSrc());
                    	axImageDO.setType("4");
                    	axImageDO.setCreatedate(new Date());
                    	axImageDao.save(axImageDO);
                    }
                }
            }
        }
		return sum;
	}
	
}
